package com.jxmcloud.business.cut.service.impl.database;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxmcloud.business.cut.mapper.database.IUserBagDBMapper;
import com.jxmcloud.business.cut.model.database.DBUserBag;
import com.jxmcloud.business.cut.service.database.IUserBagDBService;
import org.springframework.stereotype.Service;

@Service
public class UserBagDBServiceImpl extends ServiceImpl<IUserBagDBMapper, DBUserBag> implements IUserBagDBService {
    @Override
    public DBUserBag loadBag(Integer uid, Integer activityId) {
        QueryWrapper<DBUserBag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        queryWrapper.eq("activity_id", activityId);
        return this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    public DBUserBag loadBag(Integer uid) {
        QueryWrapper<DBUserBag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        return this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean updateBag(Integer uid, Integer activityId, String bag) {
        QueryWrapper<DBUserBag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        queryWrapper.eq("activity_id", activityId);
        DBUserBag userBag = this.baseMapper.selectOne(queryWrapper);
        if (userBag != null) {
            userBag.setBag(bag);
            return this.updateById(userBag);
        }
        return false;
    }

    @Override
    public boolean insertBag(Integer uid, Integer activityId, String bag) {
        DBUserBag userBag = new DBUserBag();
        userBag.setUid(uid);
        userBag.setActivityId(activityId);
        userBag.setBag(bag);
        return this.save(userBag);
    }
}
